//
//  ZZCEmoticonKeyBoard.swift
//  weibo
//  Created by zhangzuchao on 2017/4/6.
//  Copyright © 2017年 zhangZuChao. All rights reserved.
//

import UIKit

class ZZCEmoticonKeyBoard: UIView {

    
    //  toolbar
    fileprivate lazy var toolBar: ZZCEmoticonToolBar = ZZCEmoticonToolBar()
    
    //  表情视图
    fileprivate lazy var emoticonCollectionView: UICollectionView = {
        let flowLayout = UICollectionViewFlowLayout()
        //  设置水平方向滚动
        flowLayout.scrollDirection = .horizontal
        let collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: flowLayout)
        //  设置背景色
        collectionView.backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        //  启动分页
        collectionView.isPagingEnabled = true
        //  隐藏滚动指示器
        collectionView.showsVerticalScrollIndicator = false
        collectionView.showsHorizontalScrollIndicator = false
        //  去掉弹簧效果
        collectionView.bounces = false
        //  注册cell
        collectionView.register(ZZCEmoticonViewCell.self, forCellWithReuseIdentifier: EmoticonViewCellIdentifier)
        
        //  设置数据代理
        collectionView.dataSource = self
        //  设置代理
        collectionView.delegate = self
        
        return collectionView
    }()
    
    //  页数指示器
    fileprivate lazy var pageControl: UIPageControl = {
        let pageCtr = UIPageControl()

        //  使用kvc给私有成员变量赋值
        pageCtr.setValue(UIImage(named: "compose_keyboard_dot_normal")!, forKey: "_pageImage")
        pageCtr.setValue(UIImage(named: "compose_keyboard_dot_selected")!, forKey: "_currentPageImage")
        
        //  如果是单页的就不显示页数指示器
        pageCtr.hidesForSinglePage = true
        return pageCtr
    }()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //  添加控件设置约束
    fileprivate func setupUI() {
        
        //  默认滚动默认表情这组数据
        let normalIndexPath = IndexPath(item: 0, section: 1)
        //  主线程异步
        //  等到加载完成数据后表情视图有数据了才能滚动
        DispatchQueue.main.async {
            self.emoticonCollectionView.scrollToItem(at: normalIndexPath, at: [], animated: false)
            
            //绑定页面指示图,显示页数
            self.setPageControlData(indexPath: normalIndexPath)
        }
        
        
        addSubview(emoticonCollectionView)
        addSubview(pageControl)
        addSubview(toolBar)
        //  设置约束
        emoticonCollectionView.snp.makeConstraints { (make) in
            make.top.leading.trailing.equalTo(self)
            make.bottom.equalTo(toolBar.snp.top)
        }
        
        pageControl.snp.makeConstraints { (make) in
            make.bottom.equalTo(emoticonCollectionView)
            make.centerX.equalTo(emoticonCollectionView)
            make.height.equalTo(10)
        }
        
        toolBar.snp.makeConstraints { (make) in
            make.bottom.leading.trailing.equalTo(self)
            make.height.equalTo(35)
        }
        
        //  设置点击表情按钮的闭包
        toolBar.didSelectedEmoticonClosure = { [weak self] (type: ZZCEmoticonType) in
            let indexPath: IndexPath
            switch type {
            //表情视图滚动到指定的IndexPath
            case .recent:
//                print("最近")
                indexPath = IndexPath(item: 0, section: 0)
            case .normal:
//                print("默认")
                indexPath = IndexPath(item: 0, section: 1)
            case .emoji:
//                print("emoji")
                indexPath = IndexPath(item: 0, section: 2)
            case .lxh:
//                print("浪小花")
                indexPath = IndexPath(item: 0, section: 3)
            }
            //  滚动到指定indexPath
            self?.emoticonCollectionView.scrollToItem(at: indexPath, at: [], animated: false)
            
        }
    }
    
    //  根据indexPath绑定数据
    fileprivate func setPageControlData(indexPath: IndexPath) {
        
        //  获取某种表情的页数
        self.pageControl.numberOfPages =
            ZZCEmoticonTools.sharedTools.allEmoticonArray[indexPath.section].count
        
        //  当前选中的页数索引
        self.pageControl.currentPage = indexPath.item
    }

    //  刷新最近表情数据的方法
    func reloadRecentData() {
        //  重新加载最近这组表情数据
        let indexPath = IndexPath(item: 0, section: 0)
        emoticonCollectionView.reloadItems(at: [indexPath])
        
    }
    
    //  获取表情视图的大小
    override func layoutSubviews() {
        super.layoutSubviews()
        //  获取布局对象
        let flowLayout = emoticonCollectionView.collectionViewLayout as! UICollectionViewFlowLayout
        //  设置每项的大小等于表情视图的大小
        flowLayout.itemSize = emoticonCollectionView.size
        //  垂直方向cell的之间的间距
        flowLayout.minimumLineSpacing = 0
        //  水平方向cell的之间的间距
        flowLayout.minimumInteritemSpacing = 0
    }
}



 //数据源方法  滚动代理方法

extension ZZCEmoticonKeyBoard: UICollectionViewDataSource, UICollectionViewDelegate {
    
    //  三维数组中 二维数组的个数
    //  返回多少组
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return ZZCEmoticonTools.sharedTools.allEmoticonArray.count
    }
    
    
    //  二维数组中 一维数组的个数
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return ZZCEmoticonTools.sharedTools.allEmoticonArray[section].count
    }
    
    
    //   创建cell
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: EmoticonViewCellIdentifier, for: indexPath) as! ZZCEmoticonViewCell
        
        //  拿到这页20个表情数据的一维数组
        let emoticonArray = ZZCEmoticonTools.sharedTools.allEmoticonArray[indexPath.section][indexPath.item]
        
        //  绑定每页数据
        cell.emoticonArray = emoticonArray

        return cell
    }
    
    
    //代理方法  监听cell的滚动,选中对应的表情按钮
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        //  1. 如何水平方向的内容的中心点
        let contentCenterX = scrollView.contentOffset.x + scrollView.width * 0.5
        //  2. 根据内容中心获取cell对应的indexpath
        if let indexPath = emoticonCollectionView.indexPathForItem(at: CGPoint(x: contentCenterX, y: scrollView.height * 0.5)) {
            
            //  获取对应组的索引
            let section = indexPath.section
            
            //  根据索引选中对应的按钮
            toolBar.selectButton(section: section)
            
            //刷新页数指示器
            self.setPageControlData(indexPath: indexPath)
            
        }
        
    }
    
    //  NB知识点 可以用kvc给隐藏属性赋值,不让赋也能强赋上 保留备查
    //  使用runtime查看UIPageControl的私有属性
    func pageControlIVarName() {
        //  类型里面成员变量的个数
        var outCount: UInt32 = 0
        //  获取类型里面的成员变量
        if let ivarList = class_copyIvarList(UIPageControl.self, &outCount) {
            //  变量成员变量的个数获取成员变量名称
            for i in 0..<outCount {
                //  根据下标获取对应的成员的变量参数
                let ivar = ivarList[Int(i)]
                //  获取变量的名字
                let cName = ivar_getName(ivar)!
                //  C语言字符串转String类型
                let name = String(cString: cName)
                print(name)
                let cType = ivar_getTypeEncoding(ivar)!
                //  获取变量对应的类型
                let type = String(cString: cType)
                print(type)
            }
        }
    }
    

}














